package com.seean.easy.constant;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author seean
 * @date 2020/09/14 14:00
 */
@Builder
@ToString
@Accessors(chain = true)
@AllArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @JsonProperty("Code")
    private int code = CommonConstant.SUCCESS;

    @Getter
    @Setter
    @JsonProperty("Message")
    private String msg = "success";

    @Getter
    @Setter
    @JsonProperty("Data")
    private T data;

    @Getter
    @Setter
    @JsonProperty("ServerTime")
    private String serverTime;

    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public R() {
        super();
    }

    public R(T data) {
        super();
        this.data = data;
        setServerTime(df.format(new Date()));
    }

    public R(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
        setServerTime(df.format(new Date()));
    }

    public R(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = CommonConstant.FAIL;
        setServerTime(df.format(new Date()));
    }
}
